/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;

import com.foreveross.bsl.auth.service.TicketApplication;
import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.common.utils.enums.OperatEnum;
import com.foreveross.bsl.common.utils.exception.ApplicationException;
import com.foreveross.bsl.system.application.ApplicationRegisterApplication;
import com.foreveross.bsl.system.application.vo.ApplicationRegisterVO;
import com.foreveross.bsl.system.model.domain.Account;
import com.foreveross.bsl.system.model.domain.ApplicationRegister;


/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-7-22
 *
 */
@Named("applicationRegisterApplication")
public class ApplicationRegisterApplicationImpl implements ApplicationRegisterApplication {

	@Inject
	TicketApplication ticketApplication;
	
	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public ApplicationRegisterVO saveEntity(ApplicationRegisterVO entity) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		BeanHelper.copyProperties(applicationRegister, entity);
		applicationRegister.setCreateDate(new Date());
		applicationRegister.save();
		entity.setId(applicationRegister.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public ApplicationRegisterVO updateEntity(String id, ApplicationRegisterVO entity) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		applicationRegister = (ApplicationRegister)applicationRegister.findOne(id);
		BeanHelper.copyProperties(applicationRegister, entity);
		applicationRegister.save();
		BeanHelper.copyProperties(entity, applicationRegister);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(String id) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		applicationRegister = (ApplicationRegister)applicationRegister.findOne(id);
		applicationRegister.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public ApplicationRegisterVO getEntity(String id) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		applicationRegister = (ApplicationRegister)applicationRegister.findOne(id);
		ApplicationRegisterVO vo = new ApplicationRegisterVO();
		BeanHelper.copyProperties(vo, applicationRegister);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<ApplicationRegisterVO> findAll() {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		Iterable iterable = applicationRegister.findAll();
		if(iterable != null){
			List<ApplicationRegisterVO> result = new ArrayList<ApplicationRegisterVO>();
			ApplicationRegisterVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				ApplicationRegister obj = (ApplicationRegister) iterator.next();

				vo = new ApplicationRegisterVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<ApplicationRegisterVO> findByPageable(Pageable pageable) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		Page page = applicationRegister.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<ApplicationRegisterVO> result = new ArrayList<ApplicationRegisterVO>();
			ApplicationRegisterVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				ApplicationRegister obj = (ApplicationRegister) iterator.next();

				vo = new ApplicationRegisterVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<ApplicationRegisterVO> pageVO = new PageImpl<ApplicationRegisterVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.ApplicationRegisterApplication#generateAppKey(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public String generateAppKey(String accountId, String username, String appId,
			String appName, String appDesc, String appStatus) {
		Account account = new Account();
		account = (Account)account.findOne(accountId);
		if(account == null){
			throw new ApplicationException("", "此帐户不存在。");
		}
		
		ApplicationRegister ar = new ApplicationRegister();
		List arList = ar.findListByProperty(new String[]{"accountId", "appId"}, new OperatEnum[]{OperatEnum.EQ, OperatEnum.EQ}, new String[]{accountId, appId});
		if(arList !=null && !arList.isEmpty()){
			return ((ApplicationRegister)arList.get(0)).getAppKey();
		}
		
		ar.setAccountId(accountId);
		ar.setUsername(username);
		ar.setAppDesc(appDesc);
		ar.setAppName(appName);
		ar.setAppStatus(appStatus);
		ar.setAppId(appId);
		ar.setCreateDate(new Date());
		
		String appKey = null;
		Md5PasswordEncoder md = new Md5PasswordEncoder();
		appKey = md.encodePassword(account.getUsername(), appName);
		List list = ar.findListByProperty("appKey", OperatEnum.EQ, appKey);
		Random rd = new Random();
		while (list != null && list.size() > 0) {
			appKey = md.encodePassword(account.getUsername(), appName + rd.nextInt());
			list = ar.findListByProperty("appKey", OperatEnum.EQ, appKey);
		}
		ar.setAppKey(appKey);
		ar.save();
		return appKey;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.ApplicationRegisterApplication#getByAppKey(java.lang.String)
	 */
	@Override
	public ApplicationRegisterVO getByAppKey(String appKey) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		List list = applicationRegister.findListByProperty("appKey", OperatEnum.EQ, appKey);
		if(list != null && !list.isEmpty()){
			ApplicationRegisterVO vo = new ApplicationRegisterVO();
			BeanHelper.copyProperties(vo, list.get(0));
			return vo;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.ApplicationRegisterApplication#getAppIdByAppKey(java.lang.String)
	 */
	@Override
	public String getAppIdByAppKey(String appKey) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		List list = applicationRegister.findListByProperty("appKey", OperatEnum.EQ, appKey);
		if(list != null && !list.isEmpty()){
			ApplicationRegister a = (ApplicationRegister)list.get(0);
			return a.getAppId();
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.ApplicationRegisterApplication#getAppKeyByAppId(java.lang.String)
	 */
	@Override
	public String getAppKeyByAppId(String appId) {
		ApplicationRegister applicationRegister = new ApplicationRegister();
		List list = applicationRegister.findListByProperty("appId", OperatEnum.EQ, appId);
		if(list != null && !list.isEmpty()){
			ApplicationRegister a = (ApplicationRegister)list.get(0);
			return a.getAppKey();
		}
		return null;
	}

}
